﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for DanhSachKhoanPhi.xaml
    /// </summary>
    public partial class DanhSachKhoanPhi : Window, IPhanQuyenHelper
    {
        public DanhSachKhoanPhi()
        {
            InitializeComponent();
            this.Loaded += DanhSachKhoanPhi_Loaded;
        }

        void DanhSachKhoanPhi_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);

        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void Refresh()
        {
            txtMaPhi.Text = "";
            txtTenPhi.Text = "";
        }
        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            if (txtTenPhi.Text == "")
                MessageBox.Show("Chưa nhập Tên khoản phí");
            else
            {
                KHOANPHI kp = new KHOANPHI
                {
                    TenKhoanPhi = txtTenPhi.Text
                };
                
                if (KhoanPhiBUS.ThemKhoanPhi(kp))
                {
                    Window_Loaded(sender, e);
                    MessageBox.Show("Thêm thành công");
                }
                else
                    MessageBox.Show("Thêm thất bại");
            }
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            List<KHOANPHI> lstKhoanPhi = KhoanPhiDAO.GetAllKhoanPhi();
            foreach (KHOANPHI item in lstKhoanPhi)
            {
                if (item.IsChecked)
                {
                    if (KhoanPhiBUS.XoaKhoanPhi(item))
                    {

                        //MessageBox.Show("Xóa thành công");
                    }
                    else
                    {
                        MessageBox.Show("Xoá thất bại");
                        return;
                    }
                }
            }
            Window_Loaded(sender, e);
            Refresh();
            MessageBox.Show("Xóa thành công");
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaPhi.Text == "")
                return;
            KHOANPHI kp = new KHOANPHI
            {
                MaKhoanPhi = int.Parse(txtMaPhi.Text),
                TenKhoanPhi = txtTenPhi.Text
            };
            
            if (KhoanPhiBUS.CapNhatKhoanPhi(kp))
            {
                Window_Loaded(sender, e);
                Refresh();
                MessageBox.Show("Cập nhật thành công");
            }
            else
                MessageBox.Show("Cập nhật thất bại");
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<KHOANPHI> dt = KhoanPhiDAO.GetAllKhoanPhi();
            dgDanhSachKhoanPhi.ItemsSource = dt;
        }

        private void dgDanhSachKhoanPhi_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = dgDanhSachKhoanPhi.SelectedIndex;
            List<KHOANPHI> lstKhoanPhi = KhoanPhiDAO.GetAllKhoanPhi();
            if (index < 0 || index >= lstKhoanPhi.Count)
                return;
            lstKhoanPhi[index].IsChecked = !lstKhoanPhi[index].IsChecked;

            dgDanhSachKhoanPhi.ItemsSource = lstKhoanPhi;
            if (lstKhoanPhi[index].IsChecked)
            {
                txtMaPhi.Text = lstKhoanPhi[index].MaKhoanPhi.ToString();
                txtTenPhi.Text = lstKhoanPhi[index].TenKhoanPhi;                
            }
        }

        private void dgDanhSachKhoanPhi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dgDanhSachKhoanPhi.SelectedIndex;
            List<KHOANPHI> lstKhoanPhi = KhoanPhiDAO.GetAllKhoanPhi();
            if (index < 0 || index >= lstKhoanPhi.Count)
                return;
            lstKhoanPhi[index].IsChecked = !lstKhoanPhi[index].IsChecked;

            dgDanhSachKhoanPhi.ItemsSource = lstKhoanPhi;
            if (lstKhoanPhi[index].IsChecked)
            {
                txtMaPhi.Text = lstKhoanPhi[index].MaKhoanPhi.ToString();
                txtTenPhi.Text = lstKhoanPhi[index].TenKhoanPhi;
            }
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = false;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }
    }
}
